<?php
class Pageley_Commerce_Command_RegisterCustomer extends Pageley_Basic_Command_Save  {

    public function __construct($unitOfWork) {
    	parent::__construct($unitOfWork);
    	$this->set("typeName", Pageley_Commerce_Domain_Customer::type()->getFullName());
    }
    
    protected function executeInternal() {
        $properties = $this->getProperties();
        $password = $properties->get("password");
        if($password !== null && $password != $properties->get("repeatPassword")) {
            throw new Pageley_Domain_Exception(new Pageley_I18n_Message("error.validation.fieldsDoNotMatch", array("{field.password}", "{field.repeatPassword}")));
        }
        $modelAndView = parent::executeInternal();
        $customer = $modelAndView->getModel()->get("entity");
        $customer->set("status", Pageley_Commerce_Domain_CustomerStatus::$ACTIVE);
        $this->getSession()->set(Pageley_Commerce_Session::CUSTOMER, $customer);
        $modelAndView->getModel()->put("message", "registrationCompleted");
        $modelAndView->getModel()->put("message.parameters", new Pageley_Util_ArrayMap(array(
            $customer->get("username"),
            $customer->get("emailAddress"))));
        return $modelAndView;
    }
}